<?php
class DienstController extends AdminController{
	public function actionIndex(){
		$mD = new Mapper_Dienst();
		$this->diensten = $mD->getAll();
	}

	public function actionAdd(){
		$mDC = new Mapper_DienstCategorie();

		if($this->request->isPost()){
			$mD = new Mapper_Dienst();
				
			$dienst = new Dienst();
			if($dienst = $this->request->createFromPost($dienst)){
				$dienstCategorie = $mDC->get($dienst->dienst_categorie_id);
				if($dienstCategorie instanceof DienstCategorie){
					if($mD->insert($dienst)){
						$this->redirect($this->module, $this->controller);
					}
				}
			}
		}

		$this->categorien = $mDC->getAll();
	}

	public function actionRemove(){
		if($this->request->hasParam("dienst_id")){
			$mD = new Mapper_Dienst();
				
			$this->dienst = $mD->get($this->request->getParam("dienst_id"));
				
			if($this->dienst->IsInUse){
				$this->redirect($this->module, $this->controller);
			}
				
			if($this->request->hasParam("confirm")){
				try{
					if($mD->remove($this->dienst)){
						$this->redirect($this->module, $this->controller);
					}
				}catch(Exception $e){
						
				}
			}
		}else{
			$this->redirect($this->module, $this->controller);
		}
	}

	public function actionEdit(){
		if($this->request->hasParam("dienst_id")){
			$mD = new Mapper_Dienst();
			$mDC = new Mapper_DienstCategorie();
				
			$this->dienst	= $mD->get($this->request->getParam("dienst_id"));
			$prevDienst		= $mD->get($this->request->getParam("dienst_id"));
			
			if($this->request->isPost()){
				$dienst = $this->request->createFromPost($this->dienst);

				$dienstCategorie = $mDC->get($dienst->dienst_categorie_id);
				
				if($dienstCategorie instanceof DienstCategorie){
					
					if($mD->update($dienst,$prevDienst)){
						$this->redirect($this->module, $this->controller);
					}
				}
			}
			
			$this->categorien = $mDC->getAll();
		}else{
			$this->redirect($this->module, $this->controller);
		}
	}
}